
package com.booking.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;

import com.booking.R;
import com.booking.fragment.BasicFragment;
import com.booking.fragment.BasicFragment.FragmentType;
import com.booking.fragment.FavoriteFragment;
import com.booking.fragment.HomeFragment;
import com.booking.fragment.PlacesContainerFragment;
import com.booking.fragment.ServicesFragment;
import com.booking.service.GPSService;
import com.booking.service.GPSService.LocalBinder;
import com.booking.utils.InterfaceHelper.GPSHelper;
import com.booking.utils.InterfaceHelper.HomeActionListener;

public class HomeActivity extends BaseActivity implements HomeActionListener, OnClickListener, GPSHelper {
    private BasicFragment mFragment;
    private GPSService mService;
    boolean mBound = false;

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentType home = FragmentType.HOME;
        showMainFragment(home);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, GPSService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    private void showMainFragment(FragmentType type) {
        if (type == null) {
            return;
        }
        if (mFragment != null && mFragment.getFragmentType() == type) {
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag(type.name());
        if (fragment == null) {
            fragment = createFragment(type);
            if (fragment == null) {
                return;
            }
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (mFragment != null) {
                transaction.detach(mFragment);
            }
            mFragment = (BasicFragment) fragment;
            transaction.add(R.id.current_fragment, mFragment, mFragment.getFragmentType().name());
            transaction.commit();
        } else {
            BasicFragment basicFragment = (BasicFragment) fragment;
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (mFragment != null) {
                transaction.detach(mFragment);
            }
            mFragment = basicFragment;
            transaction.attach(mFragment);
            transaction.commit();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(mConnection);
    }

    private Fragment createFragment(FragmentType type) {
        if (type == null) {
            return null;
        }
        switch (type) {
            case HOME:
                return new HomeFragment();
            case SERVICES_LIST:
                return new ServicesFragment();
            case FAVORITE:
                return new FavoriteFragment();
            case PLACES_CONTAINER:
                return new PlacesContainerFragment();
            default:
                return null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onShowFragment(FragmentType fragmentType) {
        showMainFragment(fragmentType);
    }

    @Override
    public void onBackPressed() {
        FragmentType fragmentType = mFragment.getFragmentType();
        if (fragmentType != FragmentType.HOME) {
            showMainFragment(FragmentType.HOME);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View view) {
    }

    @Override
    public Location getLocation() {
        return mService.getLocation();
    }

    @Override
    public double getLatitude() {
        return mService.getLatitude();
    }

    @Override
    public double getLongitude() {
        return mService.getLongitude();
    }
}
